package com.tanhua.manage.service;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanhua.manage.mapper.AdminUserMapper;
import com.tanhua.manage.pojo.AdminUser;
import com.tanhua.manage.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Au
 * @DATE 2020/12/14 0:25
 **/
@Service
public class AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;

    @Value("${ht.jm}")
    private String htJm;

    public List<AdminUser> queryAdminUser() {
        return adminUserMapper.findAll();
    }

    public boolean deleteAdminUserById(Long id) {
        return adminUserMapper.deleteById(id) > 0;
    }

    public boolean saveAdminUser(AdminUser adminUser) {
        adminUser.setPassWord(SecureUtil.md5(adminUser.getPassWord() + htJm));
        return adminUserMapper.insert(adminUser) > 0;
    }

    public boolean updateAdminUser(Long adminUserId, String phone, Long roleId) {
        AdminUser adminUser = adminUserMapper.selectById(adminUserId);

        LambdaUpdateWrapper<AdminUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AdminUser::getPhone, phone);
        updateWrapper.set(AdminUser::getRoleId, roleId);
        updateWrapper.eq(AdminUser::getId, adminUserId);

        return adminUserMapper.update(adminUser, updateWrapper) > 0;
    }

    public boolean resetPassWord(Long adminUserId, String passWord) {
        AdminUser adminUser = adminUserMapper.selectById(adminUserId);
        LambdaUpdateWrapper<AdminUser> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.set(AdminUser::getPassWord, SecureUtil.md5(adminUser.getPassWord() + htJm));
        updateWrapper.eq(AdminUser::getId, adminUserId);

        return adminUserMapper.update(adminUser, updateWrapper) > 0;
    }

    public List<AdminUser> queryAdminUserAdminUserName(String adminUserName) {

        List<AdminUser> adminUser = adminUserMapper.queryAdminUserLikeAdminUserName(adminUserName);
        return adminUser;
    }
}
